package com.blank.net;

import com.blank.net.Interceptor.HttpCacheInterceptor;
import com.blank.net.Interceptor.HttpHeaderInterceptor;
import com.blank.net.config.NetAppContext;
import com.blank.net.config.NetConfig;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Android Studio
 * Date：2017/5/5.
 * 接口解析封装类
 */

public class RestPool {
    private RestPool() {
    }

    public static final RestPool getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public static OkHttpClient getOkHttpClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // log拦截器  打印所有的log
        if (BuildConfig.DEBUG) {
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        } else {
            logging.setLevel(HttpLoggingInterceptor.Level.NONE);
        }
        File cacheFile = new File(NetAppContext.INSTANCE.getContext().getCacheDir(), "cache");
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 100);//100M

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.readTimeout(90, TimeUnit.SECONDS)
                .connectTimeout(90, TimeUnit.SECONDS)
                .writeTimeout(90, TimeUnit.SECONDS) //设置超时
                .addInterceptor(logging)
                .addNetworkInterceptor(new HttpCacheInterceptor())
                .addInterceptor(new HttpHeaderInterceptor())
                // 尝试解决io异常
                .retryOnConnectionFailure(true)
                .cache(cache);

        //忽略https证书错误问题
        builder.hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        builder.sslSocketFactory(TrustAllSSLSocketFactory.create());

        for (int i = 0; i < NetConfig.Companion.getInterceptors().size(); i++) {
            builder.addInterceptor(NetConfig.Companion.getInterceptors().get(i));
        }

//        if (!App.getInstance().isRelease()) {
////            builder.addNetworkInterceptor(new StethoInterceptor());
////        }
        OkHttpClient client = builder.build();
        return client;
    }

    public final <T> T getService(Class<T> cls) {

        OkHttpClient client = getOkHttpClient();

        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(NetConfig.Companion.getBaseUrl())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
        return retrofit.create(cls);
    }

    private static class SingletonHolder {
        //单例对象实例
        private static final RestPool INSTANCE = new RestPool();
    }
}
